package com.yami.shop.admin.controller;

import com.yami.shop.bean.app.dto.OrderStaticsDto;
import com.yami.shop.common.util.Arith;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.mp.enums.AiLangType;
import org.aspectj.weaver.ast.Or;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/home/statitics")
@Api(tags = "首页统计图")
@AllArgsConstructor
public class HomeController {

    private final OrderService orderService;

    /**
     * 订单详情信息接口
     */
    @GetMapping("/census")
    @ApiOperation(value = "订单详情信息", notes = "根据订单号获取订单详情信息")
    public ResponseEntity<Map> orderDetail() {
       Long shopId = SecurityUtils.getSysUser().getShopId();
       ResponseEntity<Map> map = null;
        HashMap hashMap = new HashMap();
        /**
         * 查询今日订单总数，今日销售总额
         */
       List<OrderStaticsDto> tdTotalSales = orderService.selectTodayStatics(shopId);
       if(tdTotalSales.size()==0||null==tdTotalSales){
           tdTotalSales.add(new OrderStaticsDto());
       }
       hashMap.put("tdTotalSales",tdTotalSales);
        /**
         * 查询昨日订单总数，昨日销售总额
         */
        List<OrderStaticsDto> ydTotalsales = orderService.selectYesterdayStatics(shopId);
        if(ydTotalsales.size()==0||ydTotalsales==null){
            ydTotalsales.add(new OrderStaticsDto());
        }
        hashMap.put("ydTotalsales",ydTotalsales);
        /**
         * 查询待付款订单，待发货订单,已完成订单
         */
        List<OrderStaticsDto> pendingPays = orderService.selectPendingOrder(shopId);
        if(pendingPays.size() == 0||null==pendingPays){
            pendingPays.add(new OrderStaticsDto());
        }
        hashMap.put("pendingPays",pendingPays);
        /**
         * 查询待发货订单
         */
        List<OrderStaticsDto> pendingTransfer = orderService.selectTransOrder(shopId);
        if(pendingTransfer.size()==0||null==pendingTransfer){
            pendingTransfer.add(new OrderStaticsDto());
        }
        hashMap.put("pendingTransfer",pendingTransfer);
        /**
         * 查询已完成订单
         */
        List<OrderStaticsDto> completedOrders = orderService.selectCompletedOrder(shopId);
        if(null == completedOrders||completedOrders.size()==0){
            completedOrders.add(new OrderStaticsDto());
        }
        hashMap.put("completedOrders",completedOrders);
        /**
         * 查询待处理退款订单
         */
        List<OrderStaticsDto> pendingRefund = orderService.selectRefund(shopId);
        hashMap.put("pendingRefund",pendingRefund);
        /**
         * 查询今日新增会员
         */
        List<OrderStaticsDto> dayMemberList = orderService.selectAddMember(shopId);
        hashMap.put("dayMemberList",dayMemberList);
        /**
         * 查询本月新增会员
         */
        List<OrderStaticsDto> monthMemberList = orderService.selectmonthMember(shopId);
        hashMap.put("monthMemberList",monthMemberList);
        /**
         * 查询会员总数
         */
        List<OrderStaticsDto> allMemberList = orderService.selectAllMember(shopId);
        hashMap.put("allMemberList",allMemberList);

        /**
         * 查询本月订单总数
         */
        List<OrderStaticsDto> tmTotalSales = orderService.selectThisMonthStatics(shopId);
        if(tmTotalSales.size()==0||null==tmTotalSales){
            OrderStaticsDto orderStaticsDto=new OrderStaticsDto();
            orderStaticsDto.setCountMoney(0D);
            orderStaticsDto.setCountNumber(0);
            tmTotalSales.add(orderStaticsDto);
        }
        hashMap.put("tmTotalSales",tmTotalSales);
        int thisMonthNumber = tmTotalSales.get(0).getCountNumber();
        double thisMonthCount = tmTotalSales.get(0).getCountMoney();

        /**
         * 查询上月订单总数
         */
        List<OrderStaticsDto> lmTotalSales = orderService.selectLastMonthStatics(shopId);
        if(lmTotalSales.size()==0||null==lmTotalSales){
            OrderStaticsDto orderStaticsDto=new OrderStaticsDto();
            orderStaticsDto.setCountMoney(0D);
            orderStaticsDto.setCountNumber(0);
            lmTotalSales.add(orderStaticsDto);
        }
        hashMap.put("lmTotalSales",lmTotalSales);
        int lastMonthNumber = lmTotalSales.get(0).getCountNumber();
        double lastMonthCount = lmTotalSales.get(0).getCountMoney();

        /**
         * 查询本周订单总数
         */
        List<OrderStaticsDto> twTotalSales = orderService.selectThisWeekStatics(shopId);
        if(twTotalSales.size()==0||null==twTotalSales){
            OrderStaticsDto orderStaticsDto=new OrderStaticsDto();
            orderStaticsDto.setCountMoney(0D);
            orderStaticsDto.setCountNumber(0);
            twTotalSales.add(orderStaticsDto);
        }
        hashMap.put("twTotalSales",twTotalSales);
        int thisWeekNumber = twTotalSales.get(0).getCountNumber();
        double thisWeekCount = twTotalSales.get(0).getCountMoney();

        /**
         * 查询上周订单总数
         */
        List<OrderStaticsDto> lwTotalSales = orderService.selectLastWeekStatics(shopId);
        if(lwTotalSales.size()==0||null==lwTotalSales){
            OrderStaticsDto orderStaticsDto=new OrderStaticsDto();
            orderStaticsDto.setCountMoney(0D);
            orderStaticsDto.setCountNumber(0);
            lwTotalSales.add(orderStaticsDto);
        }
        hashMap.put("lwTotalSales",lwTotalSales);
        int lastWeekNumber = lwTotalSales.get(0).getCountNumber();
        double lastWeekCount = lwTotalSales.get(0).getCountMoney();

        /**
         * 本月订单同比上月百分比
         */
        double monthNumberPercentage;
        if(lastMonthNumber==0){
            monthNumberPercentage = 1;
        }else{
            monthNumberPercentage = Arith.div(Arith.sub(thisMonthNumber,lastMonthNumber),lastMonthNumber);
        }

        /**
         * 本月销售总额同比上月百分比
         */
        double monthMoneyPercentage;
        if(lastMonthCount==0){
            monthMoneyPercentage = 1;
        }else{
            monthMoneyPercentage = (thisMonthCount-lastMonthCount)/lastMonthCount;
        }

        /**
         * 本周订单同比上月百分比
         */
        double weekNumberPercentage;
        if(lastWeekNumber == 0){
            weekNumberPercentage = 1;
        }else{
            weekNumberPercentage = Arith.div(Arith.sub(thisWeekNumber,lastWeekNumber),lastWeekNumber);
        }

        /**
         * 本周销售总额同比上月百分比
         */
        double weekMoneyPercentage;
        if(lastWeekCount == 0){
            weekMoneyPercentage = 1;
        }else{
            weekMoneyPercentage = (thisWeekCount-lastWeekCount)/lastWeekCount;
        }

        hashMap.put("monthNumberPercentage",monthNumberPercentage);
        hashMap.put("monthMoneyPercentage",monthMoneyPercentage);
        hashMap.put("weekNumberPercentage",weekNumberPercentage);
        hashMap.put("weekMoneyPercentage",weekMoneyPercentage);

        /**
         * 查询本周每天销售额
         */
        List<OrderStaticsDto> weekList = orderService.selectWeekDayMoney(shopId);
        hashMap.put("weekList",weekList);

        /**
         * 查询本月每天销售额
         */
        List<OrderStaticsDto> monthList = orderService.selectMonthDayMoney(shopId);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        List<Date> dates = getDateOfThisMonth();
        List<OrderStaticsDto> list = new ArrayList<>();
        for(int i = 0;i<dates.size();i++){
            String date = format.format(dates.get(i));
            OrderStaticsDto os = new OrderStaticsDto();
            if(monthList.size()==0){
                os.setTime(date);
                os.setCountMoney(0.0);
            }
            for(int j = 0 ;j<monthList.size();j++){
                String time = monthList.get(j).getTime();
                double money = monthList.get(j).getCountMoney();

                os.setTime(date);
                os.setCountMoney(0.0);

                if(time.equals(date)){
                    os.setTime(time);
                    os.setCountMoney(money);
                    break;
                }else {

                }
            }
            list.add(os);
        }
        hashMap.put("monthList",list);

        map = ResponseEntity.ok(hashMap);
       return map;
    }


    public static Date getStartDateOfCurMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getStartDateOfNextMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static List<Date> getDateOfThisMonth() {
        List<Date> dates = new ArrayList<>();
        long startDate = getStartDateOfCurMonth().getTime();
        long endDate = getStartDateOfNextMonth().getTime();
        while (startDate < endDate) {
            Date date = new Date();
            date.setTime(startDate);
            dates.add(date);
            startDate += 24 * 60 * 60 * 1000L;
        }
        return dates;
    }


}
